<?php
/**
 * Created by PhpStorm.
 * User: edmmss
 * Date: 2021/9/27
 * Time: 11:53
 */

namespace Edmmss\Common;

class Pack
{
    const PACK_FORMAT = 'NNNN';
    const PACK_STRUCT = 'Nlength/NserializerType/NserialId/Nid';  //长度, 序列化类型, 请求id, 预留id
    const PACK_HEADER_SIZE = 16;

    /**
     * 打包二进制字符串
     *
     * @date   2021/9/28
     * @param $data
     * @param $serializerType
     * @param $serialId
     * @param int $id
     * @return false|string
     * @author edmmss
     */
    public static function pack($data, $serializerType, $serialId, $id = 0)
    {
        return pack(self::PACK_FORMAT, strlen($data), $serializerType, $serialId, $id);
    }

    /**
     * 解包二进制字符串
     *
     * @date   2018/8/5
     * @param $data
     * @return array
     * @author edmmss
     */
    public static function unpack($data)
    {
        return unpack(self::PACK_STRUCT, $data);
    }
}